package 剑指offer.数的整数次方_12;

/**
 * 给定一个double类型的浮点数base和int类型的整数exponent。
 * 求base的exponent次方。
 * @author x
 * @summary
 */
public class Main {
	public static void main(String[] args){
		System.out.println(Power(2, -3));
	}
	
	public static double Power(double base, int exponent) {
        int n;
        int res = 1;
        if(exponent < 0){
        	n = -exponent;
        }else if(exponent == 0){
        	if(base == 0) throw new RuntimeException("分母不能为0");
        	else return 1;
        }else n = exponent;
        
        while(n != 0){
        	if((n & 1) == 1) res *= base;
        	base *= base;
        	n >>= 1;
        }
        return exponent > 0? res : 1.0/res;
	}
}
